
package com.bear.cmcc.NetTestHTTP.ui.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bear.cmcc.NetTestHTTP.R;
import com.bear.cmcc.NetTestHTTP.component.CityBean;
import com.bear.cmcc.NetTestHTTP.ui.view.HVListView;
import com.bear.cmcc.NetTestHTTP.util.MyCommonUtil;

public class CitySortAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;

    private HVListView mListView;

    private boolean isTD = true;

    private boolean isOpenSelect = true;

    private List<CityBean> list;

    private ViewHolder viewholder;

    private boolean[] isCheckState;

    private int showColor = 0;

    public CitySortAdapter(Context context, List<CityBean> list, HVListView mListView) {
        this.list = list;
        layoutInflater = LayoutInflater.from(context);
        this.mListView = mListView;
        showColor = MyCommonUtil.getHigthShowColor(context);
    }

    public void notifyDataSetChanged(int lenght, boolean isTD, int pos) {
        this.isTD = isTD;
        checkStateInit(lenght,pos);
        super.notifyDataSetChanged();
    }

    private void checkStateInit(int lenght, int pos) {
        isCheckState = null;
        isCheckState = new boolean[lenght];
        for (int i = 0; i < isCheckState.length; i++) {
//        	if(pos==i&& pos > -1)
//                isCheckState[i] = true;
//        	else
            isCheckState[i] = false;
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        CityBean csb = list.get(position);
        viewholder = null;
        if (convertView == null) {
            viewholder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_city_net, null);
            viewholder.lyItem = (LinearLayout) convertView.findViewById(R.id.llitem1);
            viewholder.lyIteminfo = (LinearLayout) convertView.findViewById(R.id.lyInfo);
            viewholder.cbSelect = (CheckBox) convertView.findViewById(R.id.cbSelect);
            viewholder.cbSelect.setClickable(false);
            viewholder.tvNumber = (TextView) convertView.findViewById(R.id.tvNumber);
            viewholder.tvCity = (TextView) convertView.findViewById(R.id.tvCity);
            viewholder.tvTD2G = (TextView) convertView.findViewById(R.id.tvTD2G);

//            viewholder.tvFrequency = (TextView) convertView.findViewById(R.id.tvFrequency);
            viewholder.tvDropRate = (TextView) convertView.findViewById(R.id.tvDropRate);
            viewholder.tvSignal = (TextView) convertView.findViewById(R.id.tvSignal);
            viewholder.tvComplete = (TextView) convertView.findViewById(R.id.tvComplete);
            convertView.setTag(viewholder);
        } else {
            viewholder = (ViewHolder) convertView.getTag();
        }
        
        
        if (isOpenSelect) {
            viewholder.cbSelect.setVisibility(View.VISIBLE);
        } else {
            viewholder.cbSelect.setVisibility(View.GONE);
        }
        viewholder.cbSelect.setChecked(isCheckState[position]);
        
        if (csb.isCurrentCity()) {
            convertView.setBackgroundColor(showColor);
        } else {
            convertView.setBackgroundColor(Color.TRANSPARENT);
            if (position % 2 == 0) {
                convertView.setBackgroundResource(R.drawable.bg_flutter_list);
            } else {
                convertView.setBackgroundResource(R.drawable.black_background);
            }
        }
        viewholder.tvNumber.setText(String.valueOf(position+1));
        viewholder.tvCity.setText(csb.getCity_name());

        if (isTD) {
            viewholder.tvTD2G.setText(String.valueOf(csb.getDownSpeed()));
        } else {
            viewholder.tvTD2G.setText(String.valueOf(csb.getDownSpeed()));
        }
//        viewholder.tvFrequency.setText(String.valueOf(csb.getElectricalLevel()));
        viewholder.tvDropRate.setText(String.valueOf(csb.droppingRate));
        viewholder.tvSignal.setText(String.valueOf(csb.signalStrength));
        viewholder.tvComplete.setText(csb.getComRate());

        // 校正（处理同时上下和左右滚动出现错位情况）
        View child = ((ViewGroup) convertView).getChildAt(1);
        int head = mListView.getHeadScrollX();
        if (child.getScrollX() != head) {
            child.scrollTo(mListView.getHeadScrollX(), 0);
        }
        return convertView;
    }

    class ViewHolder {
    	LinearLayout lyItem;
    	LinearLayout lyIteminfo;
        CheckBox cbSelect;
        TextView tvNumber;

        TextView tvCity;

        TextView tvTD2G;

//        TextView tvFrequency;

        TextView tvDropRate;

        TextView tvSignal;

        TextView tvComplete;
    }

    /**
     * 是否打开选择
     * 
     * @param isbool true打开，false关闭
     */
    public void setSelect(boolean isbool) {
        isOpenSelect = isbool;
        super.notifyDataSetChanged();
    }

    /**
     * 是否全选
     * 
     * @param isbool true选中，false取消
     */
    public void setAllSelect(boolean isbool) {
        for (int i = 0; i < isCheckState.length; i++) {
            isCheckState[i] = isbool;
        }
        super.notifyDataSetChanged();
    }

    /**
     * 单选
     * 
     * @param location 单选下标
     */
    public void setRadioSelect(int location) {
        if (isCheckState[location]) {
            isCheckState[location] = false;
        } else {
            isCheckState[location] = true;
        }
        super.notifyDataSetChanged();
    }
    
    public void setRadio(int location){
    	try{
        isCheckState[location] = true;
    	}catch (Exception e) {
			e.printStackTrace();
		}
        super.notifyDataSetChanged();
    }

    /**
     * 是否选中
     * 
     * @param location 行号
     * @return true选中，false未选
     */
    public boolean isSelected(int location) {
        return isCheckState[location];
    }

    /**
     * 设置高亮显示的省份。
     * 
     * @param proviceName 省份名称
     */
    public int setSelectShowItemt(String proviceName) {
        int selectShowItem = -1;
        if (proviceName != null && list != null) {
            
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setCurrentCity(false);
            }
            
            for (int i = 0; i < list.size(); i++) {
                if (proviceName.contains(list.get(i).getCity_name())) {
                    list.get(i).setCurrentCity(true);
                    selectShowItem = i;
                    break;
                }
            }
        }
        return selectShowItem;

    }
}
